package com.min.redis.common.util.redis;

/**
 * @author wangmin
 * @date 2023/12/31
 */
public enum LuaTypeEnum {
    INCR_CALL_LINE_NUMBER_CONCURRENT("cc_incrCallLineNumberConcurrent", "线路号码并发锁定", "local key=KEYS[1]\n" +
            "local count=redis.call(\"GET\",key)\n" +
            "if not(count) or tonumber(count)<tonumber(ARGV[1]) then\n" +
            "  local curCount=redis.call(\"INCR\",key)\n" +
            "  redis.call(\"EXPIRE\",key,tonumber(ARGV[2]))\n" +
            "  return curCount\n" +
            "end\n" +
            "return -count"),

    INCR("redis_incr", "redis原子增", "local key=KEYS[1]\n" +
            "local existKey=redis.call(\"GET\",key)\n" +
            "local curCount=redis.call(\"INCR\",key)\n" +
            "local boolVal = tonumber(ARGV[1])\n" +
            "if not(existKey) or boolVal == 1 then\n" +
            "  redis.call(\"EXPIRE\",key,tonumber(ARGV[2]))\n" +
            "end\n" +
            "return curCount"),

    SET_WITH_EXPIRE("redis_set_with_expire", "redis设置值并设置过期时间", "local key=KEYS[1]\n" +
            "redis.call(\"set\",key, ARGV[1])\n" +
            "redis.call(\"EXPIRE\",key,tonumber(ARGV[2]))"),
    CLUSTER_LOCK("CULSTER_LOCK", "redis_分布式锁", "if redis.call(\"get\",KEYS[1]) == ARGV[1] then\n" +
            "    return 0\n" +
            "else\n" +
            "    redis.call(\"set\",KEYS[1],ARGV[1],ARGV[2],ARGV[3])\n" +
            "    return 1\n" +
            "end"),

    RELEASE_CALL_LINE_NUMBER_CONCURRENT("cc_releaseCallLineNumberConcurrent", "线路号码并发释放", "local key=KEYS[1]\n" +
            "local count=redis.call(\"GET\",key)\n" +
            "if count and tonumber(count)>0 then\n" +
            "  local curCount=redis.call(\"DECR\",key)\n" +
            "  return curCount\n" +
            "end\n" +
            "return -1"),



    // TODO 该脚本只用来测试
    TEST("test", "线路号码并发释放", "return {KEYS[1], ARGV[1]}");
    private String code;
    private String msg;
    private String script;



    public static void main(String[] args) {
        System.out.println(LuaTypeEnum.INCR.getScript());
    }
    LuaTypeEnum(String code, String msg, String script) {
        this.code = code;
        this.msg = msg;
        this.script = script;
    }

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public String getScript() {
        return script;
    }
}

